﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.ComponentModel;

namespace CygwinPackageManager.Common
{
	public class MirrorDALC
	{
		private const string MirrorsListUri = "http://cygwin.com/mirrors.lst";

		public IEnumerable<Mirror> GetMirrors()
		{
			string mirrorList = string.Empty;
			using (WebClient wc = new WebClient())
			{
				mirrorList = wc.DownloadString(new Uri(MirrorsListUri));
			}

			List<Mirror> mirrors = new List<Mirror>();

			Regex regexMirror = new Regex(Properties.Resources.MirrorsRegex, RegexOptions.Multiline);
			foreach (Match m in regexMirror.Matches(mirrorList))
			{
				string uri = m.Groups["Uri"].Value;
				string country = m.Groups["Country"].Value;
				string continent = m.Groups["Continent"].Value;
				Mirror mirror = new Mirror()
				{
					Uri = uri,
					Continent = continent,
					Country = country,
					Name = string.Format("{0} - {1} ({2})", continent, country, uri),
				};

				mirrors.Add(mirror);
			}

			return mirrors;
		}
	}
}
